package com.study.happystudy.service.impl;

import com.study.happystudy.mapper.FriendshipMapper;
import com.study.happystudy.po.FriendshipList;
import com.study.happystudy.service.FriendshipService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import java.util.List;

//好友关系业务实现类
@Service
public class FriendshipServiceImpl implements FriendshipService {
    @Resource
    FriendshipMapper friendshipMapper;

    /**
     * 根据用户id和状态获取好友列表
     *
     * @param uid    用户id
     * @param status 好友状态
     * @return
     */
    @Override
    public List<FriendshipList> getFriendshipsByUserId(int uid, String status) {
        try {
            List<FriendshipList> list = friendshipMapper.getFriendshipListById(uid, status);
            if (list != null) {
                return list;
            } else {
                return null;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

    }

    /**
     * 获取好友申请列表
     *
     * @param uid 用户id
     * @return
     */
    @Override
    public List<FriendshipList> getFriendshipAskList(int uid) {
        try {
            List<FriendshipList> list = friendshipMapper.getFriendshipAskList(uid);
            if (list != null) {
                return list;
            } else {
                return null;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 添加好友关系
     *
     * @param uid 用户id
     * @param fid 好友id
     * @return
     */
    @Override
    public int addFriendship(int uid, int fid) {
        try {
            int result = friendshipMapper.addFriendship(uid, fid);
            if (result > 0) {
                return result;
            } else {
                return 0;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }

    /**
     * 更新好友关系状态
     * @param friendshipId 好友关系id
     * @param status 状态
     * @return
     */
    @Override
    public int updateFriendshipStatus(int friendshipId, String status) {
        try {
            int result = friendshipMapper.updateFriendshipStatus(friendshipId, status);
            if (result > 0) {
                return result;
            } else {
                return 0;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }


}
